package com.ruoyi.notice.service.message;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.account.enums.AccountTypeEnum;
import com.ruoyi.account.enums.ActionTypeEnum;
import com.ruoyi.account.service.IAccountApplyRecordService;
import com.ruoyi.account.service.IAccountInfoService;
import com.ruoyi.db.domain.AccountApplyRecord;
import com.ruoyi.db.domain.AccountInfo;
import com.ruoyi.notice.enums.SendTypeEnum;
import com.ruoyi.notice.model.result.HandleMsgResult;
import com.ruoyi.notice.service.IHandleMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class BillEnterWarningHandleMessageService implements IHandleMessageService {

    @Autowired
    IAccountApplyRecordService accountApplyRecordService;
    @Autowired
    IAccountInfoService accountInfoService;

    @Override
    public List<HandleMsgResult> toHandleNoticeMessage(List<String> dataIdList, JSONObject extData) {

        /**
         *
         *
         */
        List<HandleMsgResult> handleMsgResultList = new ArrayList<>();
        if (CollUtil.isEmpty(dataIdList)){
            return handleMsgResultList;
        }

        /**
         *
         */
        HandleMsgResult handleMsgResult = new HandleMsgResult();

        Set<String> userIdSet = new HashSet<>();

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("以下入账申请待处理：").append("\n");
        for (String dataId : dataIdList) {
            AccountApplyRecord accountApplyRecord = accountApplyRecordService.selectAccountApplyRecordById(Long.valueOf(dataId));

            String message = StrUtil.format("【{}】【{}】【{}】【{}】",
                    AccountTypeEnum.getLabelByValue(accountApplyRecord.getItemAccountType()),
                    accountApplyRecord.getItemAccountName(),
                    ActionTypeEnum.getLabelByValue(accountApplyRecord.getActionType()),
                    accountApplyRecord.getBillAmount()
            );
            stringBuilder.append(message);

            if (ObjectUtil.isNotEmpty(accountApplyRecord.getMainAccountName())){
                stringBuilder.append(StrUtil.format("【{}】", accountApplyRecord.getMainAccountName()));
            }
            stringBuilder.append("；\n");

            AccountInfo accountInfo = accountInfoService.selectAccountInfoById(Long.valueOf(accountApplyRecord.getMainAccountId()));
            if (ObjectUtil.isNotEmpty(accountInfo)){
                userIdSet.add(accountInfo.getWeixinUserid());
            }
        }
        handleMsgResult.setContent(stringBuilder.toString());
        handleMsgResult.setSendTypeEnum(SendTypeEnum.WEIXIN_QIYE);

        List<String> userInfoList = JSON.parseArray(JSON.toJSONString(userIdSet), String.class);
        handleMsgResult.setUserInfoList(userInfoList);

        handleMsgResultList.add(handleMsgResult);

        return handleMsgResultList;
    }
}
